package cn.hxzy.course.service.impl;

import cn.hxzy.common.domain.*;
import cn.hxzy.common.domain.vo.CoursewareVo;
import cn.hxzy.common.domain.vo.FileInfoVo;
import cn.hxzy.common.domain.vo.SectionVo;
import cn.hxzy.common.domain.vo.TaskVo;
import cn.hxzy.course.mapper.*;
import cn.hxzy.course.service.SectionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author niuyi
 * @since 2024-06-22
 */
@Service
public class SectionServiceImpl extends ServiceImpl<SectionMapper, Section> implements SectionService {
    @Autowired
    private  SectionMapper sectionMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private ChapterMapper chapterMapper;
    @Autowired
    private CoursewareMapper coursewareMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private FileInfoMapper fileInfoMapper;

    @Override
    public String updateSectionName( Integer sectionId, String sectionName) {
        int i = sectionMapper.updateSectionName(sectionId, sectionName);
        if (i>0){
            return "修改成功";
        }
        return null;
    }


    @Override
    public List<Section> sectionList(Integer chapterId) {
        QueryWrapper<Section> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        List<Section> sections = sectionMapper.selectList(queryWrapper);
        return sections;
    }
    @Override
    public int increaseSection(Section section) {
        int insert = sectionMapper.insert(section);
        if (insert>0){
            return 1;
        }
        return 0;
    }

    @Override
    public int deleteById(Integer sectionId) {
        int i = sectionMapper.deleteById(sectionId);
        if (i>0){
            return 1;
        }
        return 0;
    }

    @Override
    public List<Section> getSectionByName(String sectionName) {

        List<Section> sections = sectionMapper.getSectionByName(sectionName);
        return sections;
    }

    @Override
    public List<SectionVo> getList(Integer courseId) {
        List<Chapter> chapterById = chapterMapper.getChapterById(courseId);
        List<SectionVo> list = new ArrayList();
        if (chapterById != null) {
            chapterById.forEach(chapter -> {
                QueryWrapper<Section> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("chapter_id", chapter.getChapterId());
                List<Section> sections = sectionMapper.selectList(queryWrapper);
                if (sections != null) {
                    //遍历小节，找到对应的视频集合
                    sections.forEach(section -> {
                        QueryWrapper<Video> queryWrapper1 = new QueryWrapper<>();
                        queryWrapper1.eq("section_id", section.getSectionId());
                        List<Video> videos = videoMapper.selectList(queryWrapper1);
                        SectionVo sectionVo = new SectionVo(section.getSectionId(), section.getSectionName(), section.getChapterId(), videos);
                        list.add(sectionVo);
                    });

                }
            });
            return list;
        }
        return null;
    }



    @Override
    public List<CoursewareVo> getCoursewareSectionList(Integer courseId) {
        List<Chapter> chapterById = chapterMapper.getChapterById(courseId);
        List<CoursewareVo> list = new ArrayList();
        if (chapterById != null) {
            chapterById.forEach(chapter -> {
                QueryWrapper<Section> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("chapter_id", chapter.getChapterId());
                List<Section> sections = sectionMapper.selectList(queryWrapper);
                if (sections != null) {
                    //遍历小节，找到对应的课件集合
                    sections.forEach(section -> {
                        QueryWrapper<Courseware> queryWrapper1 = new QueryWrapper<>();
                        queryWrapper1.eq("section_id", section.getSectionId());
                        List<Courseware> coursewares = coursewareMapper.selectList(queryWrapper1);
                        CoursewareVo coursewareVo = new CoursewareVo(section.getSectionId(), section.getSectionName(), section.getChapterId(), coursewares);
                        list.add(coursewareVo);
                    });

                }
            });
            return list;
        }
        return null;
    }


    @Override
    public List<TaskVo> getTaskSectionList(Integer courseId) {
        List<Chapter> chapterById = chapterMapper.getChapterById(courseId);
        List<TaskVo> list = new ArrayList();
        if (chapterById != null) {
            chapterById.forEach(chapter -> {
                QueryWrapper<Section> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("chapter_id", chapter.getChapterId());
                List<Section> sections = sectionMapper.selectList(queryWrapper);
                if (sections != null) {
                    //遍历小节，找到对应的作业集合
                    sections.forEach(section -> {
                        QueryWrapper<Task> queryWrapper1 = new QueryWrapper<>();
                        queryWrapper1.eq("section_id", section.getSectionId());
                        List<Task> tasks = taskMapper.selectList(queryWrapper1);
                        TaskVo taskVo = new TaskVo(section.getSectionId(), section.getSectionName(), section.getChapterId(), tasks);
                        list.add(taskVo);
                    });

                }
            });
            return list;
        }
        return null;
    }


    @Override
    public List<FileInfoVo> getFileInfoSectionList(Integer courseId) {
        List<Chapter> chapterById = chapterMapper.getChapterById(courseId);
        List<FileInfoVo> list = new ArrayList();
        if (chapterById != null) {
            chapterById.forEach(chapter -> {
                QueryWrapper<Section> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("chapter_id", chapter.getChapterId());
                List<Section> sections = sectionMapper.selectList(queryWrapper);
                if (sections != null) {
                    //遍历小节，找到对应的资料集合
                    sections.forEach(section -> {
                        QueryWrapper<FileInfo> queryWrapper1 = new QueryWrapper<>();
                        queryWrapper1.eq("section_id", section.getSectionId());
                        List<FileInfo> fileInfos = fileInfoMapper.selectList(queryWrapper1);
                        FileInfoVo fileInfoVo = new FileInfoVo(section.getSectionId(), section.getSectionName(), section.getChapterId(), fileInfos);
                        list.add(fileInfoVo);
                    });

                }
            });
            return list;
        }
        return null;
    }
}
